package com.lobo.blog.service.impl;

import com.lobo.blog.entity.Comment;
import com.lobo.blog.mappers.CommentMapper;
import com.lobo.blog.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * className: CommentServiceImpl<br/>
 * author: MacieSerenity <br/>
 * date: 2022-10-14 17:22
 **/
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    CommentMapper commentMapper;

    @Override
    public List<Comment> listCommentByBlogId(Integer blogId) {
        List<Comment> topCommentList=commentMapper.findByBlogIdAndParentCommentNull(blogId);
        for (Comment com:topCommentList) {
            List<Comment> commentByParentId = commentMapper.findCommentByParentId(com.getId());
            com.setReplyComments(commentByParentId);
        }
        return topCommentList;
    }

    @Override
    public Comment saveComment(Comment comment) {
        Integer parentId = comment.getParentComment().getId();
        //如果父级评论id不为-1，则表示该评论是嵌套评论
        if (parentId != -1) {
            Comment singleCommentByBlogId = commentMapper.findSingleCommentByCommentId(parentId);
            if (singleCommentByBlogId.getTopCommentId()==0){
                comment.setTopCommentId(singleCommentByBlogId.getId());
            }
            comment.setParentComment(singleCommentByBlogId);
        } else {
            //如果父级评论的id为-1，表示该评论为一级评论
            //如果是一级评论，那么就没有父级评论
            comment.setTopCommentId(0);
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        commentMapper.saveComment(comment);
        return commentMapper.findSingleCommentByCommentId(comment.getId());
    }



}
